package cn.chint.manage.dao.impl;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import cn.chint.bo.MsgEnterTempbo;
import cn.chint.bo.NoticeTemp;
import cn.chint.manage.dao.NoticeUserDao;
import cn.chint.po.MsgEnter;
import cn.chint.po.User;
@Repository
public class NoticeUserImpl implements NoticeUserDao{
	@Autowired
	HibernateTemplate hibernateTemplate;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void update(final String openid,final long id) {
		this.hibernateTemplate.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException {
				return session.createQuery("Update NoticeUser set openid=:openid" +
						" where id=:id").setString("openid", openid).setLong("id", id).executeUpdate();
			}
		});

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<NoticeTemp> select() {
		List<NoticeTemp> ls  = (List<NoticeTemp>) this.hibernateTemplate.execute(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException {
				return session.createSQLQuery("select u.wx_nickname,n.type from t_user u , notice_user  n where u.wx_open_id = n.openid")
						.addScalar("wx_nickname",StandardBasicTypes.STRING).addScalar("type",StandardBasicTypes.STRING).
						    setResultTransformer(Transformers.aliasToBean(NoticeTemp.class)).list();
			}
		});
		return ls;
	}

}
